import React, { Component } from 'react';
import { Stepper, Toast } from 'antd-mobile';
import { updateCartCount } from './api';

class CartStepper extends Component {
  constructor(props) {
    super(props);
    this.state = {
      num: '',
    };
  }


  // 更新购物车数量
  updateCart = (num) => {
    const { goodsNowStorage, cartId } = this.props;
    if (num <= goodsNowStorage) {
      // Toast.loading();
      updateCartCount({ cartId, count: num }).then(r => {
        if (r.result == 1) {             //"eqeqeq": false
          // Toast.success(r.msg, 1.5);
          this.setState({ num });
          this.props.updateModelCart(this.props.index, this.props.listIndex, num);
        } else {
          Toast.fail(r.msg, 1.5);
        }
      });
    } else {
      Toast.info('库存不足', 1);
    }
  }


  render() {
    const { goodsNowStorage, goodsNum } = this.props;
    let { num } = this.state;
     num = num == '' ? goodsNum : num;
    return (
      <Stepper style={{ width: "2rem" }} showNumber min={1} max={goodsNowStorage} value={num} onChange={this.updateCart} />
    );
  }
}

export default CartStepper;
